﻿@{
	Layout = null;
}
<link href="~/libs/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="" style="width:80%;margin:0 auto;">
	<div class="layui-form-item">
		<label class="layui-form-label">单位编码</label>
		<div class="layui-input-block">
			<input type="text" name="UnitMeasureCode" lay-verify="required" placeholder="请输入单位编码" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">单位名称</label>
		<div class="layui-input-block">
			<input type="text" name="UnitMeasureName" lay-verify="required" placeholder="请输入单位名称" autocomplete="off" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">是否主单位</label>
		<div class="layui-input-block">
			<input type="radio" name="IsPrimaryUnit" id="isPrimaryUnit" value="true" title="是" checked>
			<input type="radio" name="IsPrimaryUnit" id="isPrimaryUnit" value="false" title="否">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">单位换算比例</label>
		<div class="layui-input-block">
			<input type="text" name="ConversionScale" lay-verify="required" placeholder="请输入换算比例" autocomplete="off" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">是否启用</label>
		<div class="layui-input-block">
			<input type="radio" name="State" id="state" value="true" title="是" checked>
			<input type="radio" name="State" id="state" value="false" title="否">
		</div>
	</div>

	<div class="layui-form-item">
		<div class="layui-input-block">
			<button type="submit" class="layui-btn" lay-submit lay-filter="add">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			<button type="button" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
		</div>
	</div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

<script>

	//关闭弹框
	function closeWorkstation() {
		var index = parent.layer.getFrameIndex(window.name); //先得到当前的索引
		parent.layer.close(index); //再执行关闭
	}
	// 页面加载完成后执行的函数
	$(function () {
		$('#conversionRatio').val(9527);
	});
	layui.use(['form', 'laydate', 'util'], function () {
		var form = layui.form;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var util = layui.util;

		// 提交事件
		form.on('submit(add)', function (data) {
			var field = data.field; // 获取表单字段值
			// 发送 AJAX 请求添加数据
			$.ajax({
				url: '/UnitMeasure/CreatUnitMeasureInfo', // 替换成你的后端API添加接口URL
				type: 'POST', // 使用POST方法提交数据
				data: field, // 发送表单字段数据
				dataType: 'json', // 期望从服务器返回的数据类型
				success: function (res) {
					if (res == -1) { // 假设后端返回的成功状态码为0
						layer.alert('添加失败：编号重复');
					} else {
						layer.alert('添加成功', { title: '操作结果' }, function () {
							// 添加成功后的逻辑，例如关闭弹窗，刷新表格等
								var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
								window.parent.location.reload(); // 刷新父页
								parent.layer.close(index); //再执行关闭
								setTimeout(function () { parent.layer.close(index) }, 330);//延迟s
						});
					}
				},
				error: function (err) {
					// 处理添加失败的情况
					layer.alert('添加失败：网络异常，请稍后重试', { title: '操作结果' });
					console.error('添加失败', err);
				}
			});

			return false; // 阻止默认 form 提交
		});

	});


</script>

